//
//  UITableView+GMAdd.m
//  gentleman
//
//  Created by 范月成 on 2017/12/6.
//  Copyright © 2017年 fancy. All rights reserved.
//

#import "UITableView+GMAdd.h"
#import <objc/runtime.h>

@implementation UITableView (GMEmpty)

- (void)gm_configureWithPlaceHolderBlock:(GMPlaceHolderBlock)block
                             normalBlock:(GMPlaceHolderNormalBlock)normal{
    self.gm_placeHolderBlock = [block copy];
    if (self.gm_placeHolderBlock) {
        [self gmPlaceHolder_swizzling];
        self.gmPlaceHolder_configure = YES;
        self.gm_placeHolderNormalBlock = [normal copy];
        return;
    }
    self.gmPlaceHolder_configure = NO;
}

- (void)gmPlaceHolder_swizzling {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        SEL originalSelector = @selector(reloadData);
        SEL swizzledSelector = @selector(gm_reloadData);
        
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
        if (success) {
            class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod);
        }
    });
}

- (void)gm_reloadData {
    [self gm_reloadData];
    
    if(!self.gmPlaceHolder_isConfigured) return;
    
    BOOL isEmpty = YES;
    
    id<UITableViewDataSource> src = self.dataSource;
    NSInteger sections = 1;
    if ([src respondsToSelector: @selector(numberOfSectionsInTableView:)]) {
        sections = [src numberOfSectionsInTableView:self];
    }
    for (int i = 0; i<sections; ++i) {
        NSInteger rows = [src tableView:self numberOfRowsInSection:i];
        if (rows) {
            isEmpty = NO;
        }
    }
    if(isEmpty){
        if (self.gm_placeHolderView) {
            [self.gm_placeHolderView removeFromSuperview];
            self.gm_placeHolderView = nil;
        }
        self.gm_placeHolderView = self.gm_placeHolderBlock(self);
        if(!self.gm_placeHolderView) @throw [NSException exceptionWithName:NSGenericException
                                                                    reason:[NSString stringWithFormat:@"must return a view at the time of calling the gm_configureWithPlaceHolderBlock:: in the first block."]
                                                                  userInfo:nil];
        self.gm_placeHolderView.frame = self.bounds;
        [self addSubview:self.gm_placeHolderView];
    } else {
        [self.gm_placeHolderView removeFromSuperview];
        self.gm_placeHolderView = nil;
        if(self.gm_placeHolderNormalBlock) self.gm_placeHolderNormalBlock(self);
    }
}

#pragma mark - category property getter and setter method
- (BOOL)gmPlaceHolder_isConfigured {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}
- (void)setGmPlaceHolder_configure:(BOOL)isConfigured {
    NSNumber *boolValue = [NSNumber numberWithBool:isConfigured];
    objc_setAssociatedObject(self, @selector(gmPlaceHolder_isConfigured), boolValue, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (GMPlaceHolderBlock)gm_placeHolderBlock {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setGm_placeHolderBlock:(GMPlaceHolderBlock)block {
    objc_setAssociatedObject(self, @selector(gm_placeHolderBlock), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (GMPlaceHolderNormalBlock)gm_placeHolderNormalBlock {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setGm_placeHolderNormalBlock:(GMPlaceHolderNormalBlock)block {
    objc_setAssociatedObject(self, @selector(gm_placeHolderNormalBlock), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (UIView *)gm_placeHolderView {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setGm_placeHolderView:(UIView *)view {
    objc_setAssociatedObject(self, @selector(gm_placeHolderView), view, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


@end


@implementation UITableView (GMEstimatedHeight)

- (void)disabledEstimatedHeight {
    self.estimatedRowHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    self.estimatedSectionFooterHeight = 0;
}

@end
